Object-oriented computer programming languages, such as Java™, allow programmers to define software applications as a collection of “objects” of different types. Objects of a given type may be defined to include common data structures and/or to expose common functionality.
Some programming languages, such as Java™, allow programmers to define generic types, in addition to traditional nominal types. A generic type definition includes a declaration comprising: an identifier (i.e. ground type) and one or more formal type parameters. The formal type parameters may be utilized as type names in many contexts throughout the generic type definition.
For example, consider the Java™ generic type definition:
public class List<X>{public X getNext( ){ . . . }}.
In this example, the generic type List<X> (pronounced “List of X”) includes the ground type List and the formal type parameter X. The variable X may therefore be used in the generic type definition wherever a type is needed. For example, the generic method “public X getNext( )” returns a value of type X. Since X is a type parameter, not an actual type, its scope is limited to the generic type definition of List<X>. A programmer may use the generic type List<X> by declaring a variable of type List and supplying a type argument to substitute for type parameter X. For instance, the programmer may declare a variable of type List<String> (i.e., a “List of Strings”) or a List<Integer> (i.e., a “List of Integers”).
When a type argument is supplied for a type parameter of a generic type, the resulting type (e.g. List<String>) may be referred to herein as a parameterized type. Thus, a parameterized type indicates both a ground type (e.g. List) and type arguments for all type parameters (e.g. String for X). The members of a parameterized type are the members of the generic type with the parameterized type's type arguments substituted for the generic type's type parameters. For example, the parameterized type List<String> has a method ‘public String getNext( )’ because the generic type has a method ‘public X getNext( )’.
Thus, generic types may reduce code duplication (because a single generic type List<X> is possible rather than a type StringList with ‘public String getNext( )’, a type IntegerList with ‘public Integer getNext( )’, etc), while parameterized types may increase type safety (because the caller of getNext( ) on List<String> may receive a String rather than a general type like Object which a non-generic List would typically return and which would typically have to be cast to a more useful type).
One shortcoming of parameterized types is that they may be verbose to use. For example, some languages require that parameterized types be specified both in a variable declaration (e.g., “List<String> varname”) and in an object creation (e.g., “new List<String>( );”). Some generic types may require multiple type arguments, any of which may be parameterized types themselves. Therefore, traditional parameterized type notations may quickly become verbose and/or otherwise cumbersome for the programmer, resulting in decreased productivity and making application code less readable.